//
//  OptionsLayer.m
//
//  Source file for the options layer which gives the user
//  the option to turn on/off music and sounds
//

#import "OptionsLayer.h"
#import "MainMenuLayer.h"
#import "GameState.h"
#import "SimpleAudioEngine.h"
#import "SFHFKeychainUtils.h"

static const int kOptionsTitleSize = 104;
float EffectsLevel;

@implementation OptionsLayer

// returns a scene object containing the current layer
// this essentially loads the "screen" for a
// particular section or part of the game
+(CCScene *) scene
{
	CCScene *scene = [CCScene node];
    OptionsLayer *layer = [OptionsLayer node];
    
	[scene addChild: layer];
	
	return scene;
}

// standard iOS popup that functions as a stub
// for some of the buttons that haven't been
// implemented yet
-(void) popup
{
    UIAlertView* dialog = [[UIAlertView alloc] init];
    [dialog setDelegate:self];
    [dialog setTitle:@"Hello There!"];
    [dialog setMessage:@"You clicked one of the buttons!"];
    [dialog addButtonWithTitle:@"Yes"];
    [dialog addButtonWithTitle:@"No"];
    [dialog show];
    [dialog release];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

// display popup to be shown after pressing "Erase Scores"
// reset scores and statistics to default values
-(void) popup2
{    
    [[GameState sharedGameState] eraseScores];
    
    UIAlertView* dialog = [[UIAlertView alloc] init];
    [dialog setDelegate:self];
    [dialog setTitle:@"Status"];
    [dialog setMessage:@"Statistics and high scores have been reset."];
    [dialog addButtonWithTitle:@"Yes"];
    [dialog addButtonWithTitle:@"No"];
    [dialog show];
    [dialog release];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

// handler for when music toggle button is pressed
// toggles the music state of the app (turn on/off)
-(void) musicPressed
{
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
    BOOL musicOn = [[[GameState sharedGameState] getState:@"music"] boolValue];
    if (musicOn) {
        [[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
    }
    else {
        if (![[SimpleAudioEngine sharedEngine] isBackgroundMusicPlaying]) {
            [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"backgroundMusic.mp3" loop:YES];
        }
    }
    
    [[GameState sharedGameState] setState:@"music" value:!musicOn];
}

// handler for when sound toggle button is pressed
// toggles the sound state of the app (turn on/off)
-(void) soundPressed
{
    BOOL soundOn = [[[GameState sharedGameState] getState:@"sound"] boolValue];

    if (soundOn) {
        EffectsLevel = 0;
        [SimpleAudioEngine sharedEngine].effectsVolume = EffectsLevel;
    }
    else {

        EffectsLevel = 1;
        [SimpleAudioEngine sharedEngine].effectsVolume = EffectsLevel;
        [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
    }
    
    [[GameState sharedGameState] setState:@"sound" value:!soundOn];
}

// handler for the main menu button
-(void) mainMenuPressed
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade
             transitionWithDuration:0.3 scene:[MainMenuLayer scene]]];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

-(id) init
{
    if ((self = [super init])) {
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        
        CCSprite *bg = [CCSprite spriteWithFile:@"menuBackground.png"];
        bg.position = ccp(winSize.width / 2, winSize.height / 2);
        [self addChild:bg z:0];

        CCLabelTTF *optionsTitleLbl = [CCLabelTTF labelWithString:@"Options" fontName:@"Marker Felt" fontSize:kOptionsTitleSize];
        optionsTitleLbl.position = ccp(winSize.width / 2, winSize.height - kOptionsTitleSize);
        optionsTitleLbl.color = ccc3(255, 255, 255);
        [self addChild:optionsTitleLbl];
        
        CCMenuItemImage *musicOnBtn = [CCMenuItemImage itemWithNormalImage:@"musicOnButton.png" selectedImage:@"musicOnButtonHighlight.png"];
        
        CCMenuItemImage *musicOffBtn = [CCMenuItemImage itemWithNormalImage:@"musicOffButton.png" selectedImage:@"musicOffButtonHighlight.png"];
        
        CCMenuItemToggle *musicToggle = [CCMenuItemToggle itemWithTarget:self selector:@selector(musicPressed) items:musicOnBtn, musicOffBtn, nil];
        musicToggle.selectedIndex = [[[GameState sharedGameState] getState:@"music"] boolValue] ? 0 : 1;
        
        CCMenuItemImage *soundOnBtn = [CCMenuItemImage itemWithNormalImage:@"soundOnButton.png" selectedImage:@"soundOnButtonHighlight.png"];
        
        CCMenuItemImage *soundOffBtn = [CCMenuItemImage itemWithNormalImage:@"soundOffButton.png" selectedImage:@"soundOffButtonHighlight.png"];
        
        CCMenuItemToggle *soundToggle = [CCMenuItemToggle itemWithTarget:self selector:@selector(soundPressed) items:soundOnBtn, soundOffBtn, nil];
        soundToggle.selectedIndex = [[[GameState sharedGameState] getState:@"sound"] boolValue] ? 0 : 1;
        
        CCMenuItemImage *eraseBtn = [CCMenuItemImage itemWithNormalImage:@"eraseScoresButton.png"
                                     selectedImage:@"eraseScoresButtonHighlight.png" target:self selector:@selector(popup2)];
        
        CCMenuItemImage *menuBtn = [CCMenuItemImage itemWithNormalImage:@"mainMenuButton.png"
                                     selectedImage:@"mainMenuButtonHighlight.png" target:self selector:@selector(mainMenuPressed)];

        CCMenu *mainMenu = [CCMenu menuWithItems:musicToggle, soundToggle, eraseBtn, menuBtn, nil];
        [mainMenu alignItemsVerticallyWithPadding:30.0];
        mainMenu.position = ccp(mainMenu.position.x, 300);
        [self addChild:mainMenu];
        
    }
    
    return self;
}

@end
